<?php
namespace App\Api;

use PhalApi\Api;
use App\Domain\Order as DomainOrder;
use PhalApi\Exception\BadRequestException;

/**
 * 订单相关接口
 */
class Order extends Api {
    public function getRules() {
        return array(
            'index' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
                'curpage' => array('name' => 'curpage', 'require' => true, 'desc' => '当前页数'),
                'order_status' => array('name' => 'order_status', 'require' => false, 'desc' => '订单状态：1已支付,2(搜索值为2时，为所有退款订单，不区分具体状态，包含：2申请退款,3已退款,4拒绝退款),5待评价,6已完成'),
                'shipping_status' => array('name' => 'shipping_status', 'require' => false, 'desc' => '发货状态：1已发货'),
                'order_sn' => array('name' => 'order_sn', 'require' => false, 'desc' => '订单号'),
            ),
            'orderDetail' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
                'order_id' => array('name' => 'order_id', 'require' => true, 'desc' => '订单id'),
            ),
            'myAssembleOrder' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
                'curpage' => array('name' => 'curpage', 'require' => true, 'desc' => '当前页数'),
            ),
            'returnOrder' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
                'order_id' => array('name' => 'order_id', 'require' => true, 'desc' => '订单id'),
            ),
            'payOrder' => array(
                'openid' => array('name' => 'openid', 'require' => true, 'desc' => 'openid'),
                'price' => array('name' => 'price', 'require' => true, 'desc' => '金额'),
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
                'address_id' => array('name' => 'address_id', 'require' => true, 'desc' => '地址id'),
                'goods' => array('name' => 'goods', 'require' => true, 'desc' => '商品信息'),
                'found_id' => array('name' => 'found_id', 'require' => true, 'desc' => '拼团id'),
                'is_cart' => array('name' => 'is_cart', 'require' => true, 'desc' => '1是购物车付款，2是直接购买'),
                'user_note' => array('name' => 'user_note', 'require' => false, 'desc' => '用户留言'),
            ),
            'deliveryOrder' => array(
                'order_id' => array('name' => 'order_id', 'require' => true, 'desc' => '订单id'),
            ),
            'comment' => array(
                'order_id' => array('name' => 'order_id', 'require' => true, 'desc' => '订单id'),
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
                'goods_id' => array('name' => 'goods_id', 'require' => true, 'desc' => '商品id'),
                'content' => array('name' => 'content', 'require' => true, 'desc' => '内容'),
                'img' => array('name' => 'img', 'require' => false, 'desc' => '图片字符串格式"逗号"隔开'),
            ),
            'orderVerify' => array(
                'order_sn' => array('name' => 'order_id', 'require' => true, 'desc' => '订单编号'),
            ),
            'ordersStatus' => array(
                'user_id' => array('name' => 'user_id', 'require' => true, 'desc' => '用户id'),
            ),
        );
    }

    /**
     * 我的订单列表
     * @desc 我的订单列表
     * @return int order_id 订单id
     * @return int user_id 购买者id
     * @return int order_sn 订单编号
     * @return int total_amount 订单价格
     * @return int order_status 订单状态
     * @return int shipping_status 订单配送状态
     * @return int pay_status 支付状态(现在可能用不到，以防万一)
     * @return int goods_images 商品图片
     * @return int goods_id 商品id
     * @return string goods_name 商品名称
     * @return string spec_key_name 商品规格名称
     * @return string spec_key 商品规格key
     * @return int goods_price 商品价格
     * @return int goods_num 商品数量
     * @return int add_time 添加时间
     */
    public function index() {
        $domain = new DomainOrder();
        $data = array();
        $where = '';
        if( $this->order_status ){
            if( $this->order_status == 2 ){
                $where = ' AND o.order_status IN (2,3,4)';
            }else{
                $where = ' AND o.order_status = '.$this->order_status;
            }
        }

        if( $this->shipping_status != 3 ){
            $where .= ' AND o.shipping_status = '.$this->shipping_status;
        }

        if( $this->order_sn ){
            $where .= ' AND o.order_sn like %'.$this->order_sn.'%';
        }
        $data = $domain -> getOrderList( $this->user_id, $this->curpage, $where );
        return $data;
    }

    /**
     * 订单详情
     * @desc 订单详情
     * @return int order_sn 订单编号
     * @return int user_id 用户信息
     * @return int order_id 订单id
     * @return int order_status 订单状态
     * @return int shipping_status 配送状态
     * @return int pay_status 支付状态
     * @return string consignee 收货人
     * @return int province 省id
     * @return string province_name 省名称
     * @return int city 城市id
     * @return string city_name 城市名称
     * @return int district 区id
     * @return string district_name 区名称
     * @return string address 详细地址
     * @return int mobile 手机号
     * @return int coupon_price 优惠券使用金额
     * @return int shipping_code 物流code
     * @return int total_amount 订单总价格
     * @return string add_time 添加时间
     * @return string shipping_name 快递公司
     * @return string invoice_no 快递单号
     * @return array goods_info 商品信息
     * @return array delivery_info 快递信息
     * @return string delivery_status 快递状态：0在途中、1已揽收、2疑难、3已签收、4退签、5同城派送中、6退回、7转单
     */
    public function orderDetail() {
        $domain = new DomainOrder();
        $data = $domain -> getOrderDetail( $this->user_id, $this->order_id );
        if( empty( $data ) ){
            throw new BadRequestException('订单查询失败，请检查订单信息！', 1);
        }
        return $data;
    }

    /**
     * 我参与的拼团列表
     * @desc 我参与的拼团列表
     * @return int found_id 拼团id
     * @return int all_num 拼团总数
     * @return int team_id 拼团活动id
     * @return string nickname 团长用户名昵称
     * @return string head_pic 团长头像
     * @return string follow_user_nickname 团员用户名昵称
     * @return string follow_user_head_pic 团员头像
     * @return int join 已参团人数
     * @return int need 需多少人成团
     * @return int few 还需几人参团
     * @return int goods_id 商品id
     * @return int found_end_time 成团截止时间
     * @return int status 拼团状态0:待开团(表示已下单但是未支付)1:已经开团(团长已支付)2:拼团成功,3拼团失败
     * @return int goods_images 商品图片
     */
    public function myAssembleOrder() {
        $domain = new DomainOrder();
        $data = $domain -> getMyAssembleOrder( $this->user_id, $this->curpage );
        return $data;
    }

    /**
     * 支付+创建订单接口(支付成功后才会创建订单)
     * @desc 支付+创建订单接口(支付成功后才会创建订单)
     * @return string appId appId
     * @return string timeStamp 即当前的时间
     * @return string nonceStr 随机字符串
     * @return string package 统一下单接口返回的 prepay_id 参数值
     * @return string signType 签名算法，暂支持 MD5
     * @return string paySign 签名
     * @return string order_sn 订单编号
     */
    public function payOrder() {
        $domain = new DomainOrder();
//        $a = '[{
//	"admin_id": "3",
//	"value": [{
//		"id": "232",
//		"user_id": "70",
//		"goods_id": "59",
//		"goods_name": "小鱼干",
//		"spec_key": "1_7",
//		"prom_type": "0",
//		"market_price": "0.00",
//		"goods_price": "1.00",
//		"goods_num": 1,
//		"spec": " 1斤 白色",
//		"images": "",
//		"admin_id": "3",
//		"selected": true
//	}]
//}, {
//	"admin_id": "2",
//	"value": [{
//		"id": "231",
//		"user_id": "70",
//		"goods_id": "1",
//		"goods_name": "橘子王",
//		"spec_key": "1_7",
//		"prom_type": "0",
//		"market_price": "0.00",
//		"goods_price": "0.30",
//		"goods_num": 7,
//		"spec": " 1斤 白色",
//		"images": "http://meirixiantao1115.oss-cn-beijing.aliyuncs.com/public/upload/goods/2018/11-21/02522a2b2726fb0a03bb19f2d8d9524d.jpg",
//		"admin_id": "2",
//		"selected": true
//	}, {
//		"id": "229",
//		"user_id": "70",
//		"goods_id": "1",
//		"goods_name": "橘子王",
//		"spec_key": "2_7",
//		"prom_type": "0",
//		"market_price": "0.00",
//		"goods_price": "0.20",
//		"goods_num": 7,
//		"spec": " 2斤 白色",
//		"images": "http://meirixiantao1115.oss-cn-beijing.aliyuncs.com/public/upload/goods/2018/11-21/02522a2b2726fb0a03bb19f2d8d9524d.jpg",
//		"admin_id": "2",
//		"selected": true
//	}]
//}]';
        $order_sn = $domain->createOrder();
        $data =  \PhalApi\DI()->wechatmini->WxPay( $this->openid, $this->price, $order_sn, '测试商品购买' );
        $prepay_id = $data['package'];
        if( $this->user_note ){
            $res = $domain->saveOrder( $this->openid, $this->user_id, $this->address_id, $this->price, $this->found_id, $this->is_cart, $this->goods, $order_sn, $prepay_id, $this->user_note );
        }
        $res = $domain->saveOrder( $this->openid, $this->user_id, $this->address_id, $this->price, $this->found_id, $this->is_cart, $this->goods, $order_sn, $prepay_id );
        if( empty( $res ) ){
            throw new BadRequestException('订单插入失败,商品库存不足或活动时间已到！', 1);
        }
        $data['order_sn'] = $order_sn;
        return $data;
    }

    /**
     * 前台退款
     * @desc 前台退款改变状态
     */
    public function returnOrder() {
        $domain = new DomainOrder();
        $data = $domain -> returnOrder( $this->order_id );
        if( empty( $data ) ){
            throw new BadRequestException('订单申请退款失败', 1);
        }
        return $data;
    }

    /**
     * 订单物流查询
     * @desc 订单物流查询
     */
    public function deliveryOrder() {
        $domain = new DomainOrder();
        $data = array();
        $data = $domain -> deliveryOrder( $this->order_id );
        return $data;
    }

    /**
     * 订单评价
     * @desc 订单评价
     */
    public function comment() {
        $domain = new DomainOrder();
        $data = array();
        $data['order_id'] = $this->order_id;
        $data['user_id'] = $this->user_id;
        $data['goods_id'] = $this->goods_id;
        $data['content'] = $this->content;
        if( $this->img ){
            $data['img'] = $this->img;
        }
        $res = $domain -> comment( $data );
        if( empty( $res ) ){
            throw new BadRequestException('订单评论失败', 1);
        }
        return $res;
    }

    /**
     * 订单查询
     * @desc 订单查询
     */
    public function orderVerify() {
        $domain = new DomainOrder();
        $res = $domain -> orderVerify( $this->order_sn );
//        if( empty( $res ) ){
////            throw new BadRequestException('订单评论失败', 1);
//            $res = array();
//        }else{
//            //发送模板消息
//            $send_message = array();
//            $send_message['keyword1'] = array("value" => "我们已收到您的反馈意见，感谢您的使用", "color" => "#173177"); //商品名称
//            $send_message['keyword2'] = array("value" => "我们已收到您的反馈意见，感谢您的使用", "color" => "#173177"); //订单号
//            $send_message['keyword3'] = array("value" => $res['total_amount'], "color" => "#173177");//订单总额
//            $send_message['keyword4'] = array("value" => date('Y-m-d H:i:s',$res['add_time']), "color" => "#173177");//支付时间
//            \PhalApi\DI()->wechatmini->sendWeAppMessage( $res['openid'], $res['form_id'], 'izetQ7eYQxYpy_sPgXSq1aiYzViPT_wGtIsyvMGISo0', '', '', $send_message );
//        }
        if( empty( $res ) ){
//            throw new BadRequestException('订单评论失败', 1);
            $res = array();
        }
        return $res;
    }

    public function ordersStatus(){
        $domain = new DomainOrder();
        $res = $domain -> ordersStatus( $this->user_id );
        if( empty( $res ) ){
//            throw new BadRequestException('订单评论失败', 1);
            $res = array();
        }
        return $res;
    }
}
